package deepbitview

class DifficultyService {

    static transactional = true

    /**
     * calculates the time
     * @param difficulty
     * @param hashrate in mhashs/s
     * @return
     */
    def calculateTimeForBlock(double difficulty, double hashrate) {

        def target = 0x00000000ffff0000000000000000000000000000000000000000000000000000 / difficulty
        return Math.pow(2, 256) / (target * hashrate * 1000000)

    }

    /**
     * calculates how many coins we generate in one second
     * @param difficulty
     * @param hashrate
     * @return
     */
    def calculateCoinsForSecond(double difficulty, double hashrate) {

        def time = calculateTimeForBlock(difficulty, hashrate)

        def coins = 50 / time

        return coins
    }

    /**
     * calculates how many coins we generate in one minute
     * @param difficulty
     * @param hashrate
     * @return
     */
    def calculateCoinsForMinute(double difficulty, double hashrate) {
        return calculateCoinsForSecond(difficulty, hashrate) * 60
    }

    /**
     * calculates how many coins we generate in one hour
     * @param difficulty
     * @param hashrate
     * @return
     */
    def calculateCoinsForHour(double difficulty, double hashrate) {
        return calculateCoinsForMinute(difficulty, hashrate) * 60
    }

    /**
     * calculates how many coins we generate in one day
     * @param difficulty
     * @param hashrate
     * @return
     */
    def calculateCoinsForDay(double difficulty, double hashrate) {
        return calculateCoinsForHour(difficulty, hashrate) * 24
    }

}
